//
//  CSGXButton.m
//  CarSalesMS_GCX
//
//  Created by Tony on 2016/10/21.
//  Copyright © 2016年 Tony. All rights reserved.
//

#import "CSGXButton.h"

@implementation CSGXButton

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

+ (instancetype)gxButtonWithTitle:(NSString *)title fontSize:(CGFloat)fontSize image:(NSString *)imageName {
    //
//    CSGXButton * button = [CSGXButton gx_textButton:title fontSize:fontSize normalColor:[UIColor blackColor] selectedColor:nil];
    CSGXButton * button = [[CSGXButton alloc] init];
    button.gxlblTitle.text = title;
    button.gxlblTitle.font = [UIFont systemFontOfSize:fontSize];
    
    UIImage * img = [UIImage imageNamed:imageName];

    [img gx_cornerImageWithSize:img.size fillColor:button.backgroundColor radius:10 completion:^(UIImage *image) {
        button.gxImgView.image = image;
    }];

    return button;
}


- (void)setupUI {
    //
    self.backgroundColor = [UIColor whiteColor];
    
    // imgView
    UIImageView * imgView = [[UIImageView alloc] init];
    [self insertSubview:imgView atIndex:0];
    
    imgView.backgroundColor = [UIColor clearColor];
    
    [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self).mas_offset(2);
        make.size.mas_offset(CGSizeMake(60, 60));
    }];
    
    // titleLabel
    UILabel * gxLblTitle = [UILabel gx_labelWithText:@";ls" fontSize:15 color:[UIColor blackColor]];
    [self insertSubview:gxLblTitle atIndex:1];
    gxLblTitle.textAlignment = NSTextAlignmentCenter;
//    gxLblTitle.backgroundColor = [UIColor greenColor];
    
    [gxLblTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imgView.mas_bottom).mas_offset(5);
        make.leading.trailing.equalTo(self);
        make.bottom.equalTo(self).mas_offset(-2);
    }];
    
    //
    self.gxImgView = imgView;
    self.gxlblTitle = gxLblTitle;
}



//- (CGRect)imageRectForContentRect:(CGRect)contentRect {
//    return CGRectMake(10, 10, 60, 60);
//}
//
//- (CGRect)titleRectForContentRect:(CGRect)contentRect {
//    return CGRectMake(10, 130, contentRect.size.width, contentRect.size.height);
//}

//- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
//    
////    NSLog(@"=====%@", self);
////    NSLog(@"=====%f,%f", point.x, point.y);
//    
//    if (!CGRectContainsPoint(self.bounds, point)) {
//        NSLog(@"===no");
//        return NO;
//    }
//    
//    NSLog(@"===yes");
//    return YES;
//}

@end
